package com.pai4j.aigc.controller;


import com.pai4j.aigc.llm.LlmChatService;
import com.pai4j.common.bean.PAIResponseBean;
import com.pai4j.common.bean.PAIResponseBeanUtil;
import com.pai4j.domain.dto.aigc.ChatCompletionMessageRequest;
import com.pai4j.log.annotation.OperationLog;
import com.pai4j.remote.aigc.LLMChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;



@RefreshScope
@RestController
public class LLMChatController implements LLMChatClient {


    @Autowired
    private LlmChatService llmChatService;

    @Override
    @OperationLog(
        module = "AIGC管理",
        operation = "LLM聊天",
        description = "用户使用LLM进行聊天对话",
        recordParams = false
    )
    public PAIResponseBean<String> chat(@RequestBody ChatCompletionMessageRequest chatCompletionMessage) {

        String content = llmChatService.chat(chatCompletionMessage);
        return PAIResponseBeanUtil.success(content);
    }

    @PostMapping(value = "/aigc-service/llm/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @OperationLog(
        module = "AIGC管理",
        operation = "LLM流式聊天",
        description = "用户使用LLM进行流式聊天对话",
        recordParams = false
    )
    public SseEmitter chatStream(@RequestBody ChatCompletionMessageRequest chatCompletionMessage) {
        return llmChatService.chatStream(chatCompletionMessage);
    }
}
